﻿using Dota2WebAPI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace SteamWebAPI
{
    /// <summary>
    /// Požadavek o data uživatele.
    /// </summary>
    class SteamQuery
    {
        public SteamQuery()
        {
            this.steamAPI = "F12E71BAE29489AF6EBCC29F75865E11";
        }

        public ObservableCollection<SteamUser> GetUserFriends(Int64 user)
        {
            try
            {
                string formatedURL = String.Format("http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key={0}&steamid={1}&relationship=friend&format=xml", this.steamAPI, user);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(formatedURL);
                httpWebRequest.Method = WebRequestMethods.Http.Get;

                var response = (HttpWebResponse)httpWebRequest.GetResponse();
                string pattern = @"<steamid>(\d+)</steamid>";
                string text;
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    text = sr.ReadToEnd();
                }
                Match result = Regex.Match(text, pattern);
                ObservableCollection<SteamUser> friends = new ObservableCollection<SteamUser>();
                while (result.Success)
                {
                    string number = result.Groups[1].Value;
                    Int64 steamID = Convert.ToInt64(number);
                    friends.Add(new SteamUser(steamID));
                    result = result.NextMatch();
                }
                return friends;
            }
            catch (SystemException systemException)
            {
                throw;
            }
        }

        public SteamUser GetUserDetails(string id)
        {
            SteamUserCollection result = null;
            SteamUser user = null;
            try
            {

                Int64 id64 = SteamUser.ConvertID(id);
                string formatedURL = String.Format("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={0}&steamids={1}&format=xml", this.steamAPI, id64);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formatedURL);
                request.Method = WebRequestMethods.Http.Get;

                // Výsledek requestu
                var response = (HttpWebResponse)request.GetResponse();
                string text;

                XmlSerializer serialiser = new XmlSerializer(typeof(SteamUserCollection));
                XmlTextReader reader = new XmlTextReader(response.GetResponseStream());



                result = (SteamUserCollection)serialiser.Deserialize(reader);

            }
            catch (InvalidOperationException e)
            {
                throw;
            }
            user = result.User[0];
            user.FindFriends(id);

            Server server = new Server(this.steamAPI);
            MatchOverview overview = server.GetMatchHistory(user.Steam64);
            user.Overview = overview;

            return user;
        }


        /// <summary>
        /// Steam API klíč pro rezoluci dotazů
        /// </summary>
        private string steamAPI;
    }
}
